<template>
  <div class='my-container'>
    <van-cell-group class="myInfo" v-if="user">
      <van-cell
        class="baseInfo"
        center
        :border="false"
      >
        <van-image
          class="userImg"
          slot="icon"
          :src="currentUser.photo"
          fit="cover"
          round
        />
        <div
          class="name"
          slot="title"
        >
          {{ currentUser.name }}
        </div>
        <van-button
          class="editBtn"
          size="small"
          round
          to="/my/user-profile"
        >
          编辑资料
        </van-button>
      </van-cell>
      <van-grid
        class="interaction"
        :border="false"
      >
        <van-grid-item>
          <div slot="text">
            <div class="count">{{ currentUser. art_count }}</div>
            <div class="text">头条</div>
          </div>
        </van-grid-item>
        <van-grid-item>
          <div slot="text">
            <div class="count">{{ currentUser.follow_count }}</div>
            <div class="text">关注</div>
          </div>
        </van-grid-item>
        <van-grid-item>
          <div slot="text">
            <div class="count">{{ currentUser.fans_count }}</div>
            <div class="text">粉丝</div>
          </div>
        </van-grid-item>
        <van-grid-item>
          <div slot="text">
            <div class="count">{{ currentUser.like_count }}</div>
            <div class="text">获赞</div>
          </div>
        </van-grid-item>
      </van-grid>
    </van-cell-group>
    <div
     class="no-login"
     v-else
     @click="$router.push({
       name: 'login',
       query: {
         redirect: 'my'
       }
     })"
    >
      <div class="loginImgBg">
        <i class="iconfont icon-zhanghaodenglu"></i>
      </div>
      <div class="loginText">
        注册 / 登录
      </div>
    </div>
    <van-grid
     :column-num="2"
    >
      <van-grid-item icon-prefix="iconfont" icon="iconfont icon-shoucang" text="收藏" />
      <van-grid-item icon-prefix="iconfont" icon="iconfont icon-lishi" text="历史" />
    </van-grid>
    <van-cell title="消息通知" is-link url="" />
    <van-cell class="mb-8" title="小智同学" is-link to="" />
    <van-cell v-if="user" class="loginout" @click="loginout" title="退出登录"/>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getCurrentUser } from '@/api/user'

export default {
  name: 'myIndex',
  components: {},
  data () {
    return {
      currentUser: {} // 当前用户的登录信息
    }
  },
  created () {
    this.loadCurrentUser()
  },
  methods: {
    // 退出登录
    loginout () {
      this.$dialog.confirm({
        title: '退出提示',
        message: '确认退出当前账号吗？'
      })
        .then(() => {
          // 调用 Vuex 中的 mutations 方法，将值设置为 null，user 为 null，token 也为 null
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
        })
    },
    async loadCurrentUser () {
      const { data } = await getCurrentUser()
      this.currentUser = data.data
    }
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>
<style lang='less' scoped>
.van-cell--center {
  height: 150px;
}
.my-container {
  .myInfo {
    background-image: url("./bg.jpg");
    background-size: cover;
    color: #ffffff;
    .baseInfo {
        background-color: unset;
      }
    .userImg {
      box-sizing: border-box;
      height: 66px;
      width: 66px;
      border: 1px #ffffff solid;
      margin-right: 10px;
    }
    .name {
      color: #ffffff;
      font-size: 18px;
    }
    .editBtn {
      height: 24px;
      background-color: #ffffff;
      border: 0px;
    }
    .interaction {
      font-size: 16px;
      text-align: center;
    }
    /deep/ .van-grid-item__content {
      background-color: unset;
    }
  }
  .no-login {
    height: 230px;
    background: url('./bg.jpg');
    background-size: cover;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .loginImgBg {
      width: 90px;
      height: 90px;
      border-radius: 80px;
      background-color: rgba(255, 255, 255, 0.8);
      display: flex;
      align-items: center;
      justify-content: center;
      .icon-zhanghaodenglu {
        font-size: 60px;
        color: #2C2C2C;
      }
    }
    .loginText {
      margin-top: 14px;
      font-size: 18px;
      color: #ffffff;
    }
  }
  /deep/.van-grid {
    .van-grid-item {
      height: 80px;
      .icon-shoucang{
        color: #ed5253
      }
      .icon-lishi {
        color: #ff9d1d
      }
    }
  }
  .loginout {
    text-align: center;
    color: #d86262
  }
  .mb-8 {
    margin-bottom: 8px;
  }
}

</style>
